package com.aitaoxue.palx.im;

import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imkit.widget.provider.InputProvider;
import io.rong.imlib.RongIMClient.ErrorCode;
import io.rong.imlib.RongIMClient.SendImageMessageCallback;
import io.rong.imlib.model.Message;
import io.rong.message.ImageMessage;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.View;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.ui.ChoosePictureActivity;

//im图片能力提供者
public class PhotoCollectionsProvider extends InputProvider.ExtendProvider {

	HandlerThread mWorkThread;
	Handler mUploadHandler;
	private RongContext mContext;

	public PhotoCollectionsProvider(RongContext context) {
		super(context);

		this.mContext = context;
		mWorkThread = new HandlerThread("rongyun");
		mWorkThread.start();
		mUploadHandler = new Handler(mWorkThread.getLooper());
	}

	@Override
	public Drawable obtainPluginDrawable(Context arg0) {
		return arg0.getResources().getDrawable(R.drawable.rc_ic_picture);
	}

	@Override
	public CharSequence obtainPluginTitle(Context arg0) {
		return "相册";
	}

	@Override
	public void onPluginClick(View arg0) {
		Intent intent = new Intent(mContext, ChoosePictureActivity.class);
		intent.putExtra("max", 9);
		startActivityForResult(intent, 100);
	}

	// 根据选择完毕的图片返回值,上传文件
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 100 && data != null) {
			ArrayList<String> pathList = data.getStringArrayListExtra("data");
			if (pathList != null && pathList.size() > 0) {
				int intSize = pathList.size();
				for (int i = 0; i <= intSize - 1; i++) {
					String localStrPath = pathList.get(i);

					localStrPath = "file://" + localStrPath;
					Uri pathUri = Uri.parse(localStrPath);
					mUploadHandler.post(new MyRunnable(pathUri));
				}
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 用于显示文件的异步线程
	 */
	class MyRunnable implements Runnable {
		Uri mUri;

		public MyRunnable(Uri uri) {
			mUri = uri;
		}

		@Override
		public void run() {
			// 封装image消息的im消息
			final ImageMessage content = ImageMessage.obtain(mUri, mUri);

			if (RongIM.getInstance() != null
					&& RongIM.getInstance().getRongIMClient() != null) {
				RongIM.getInstance()
						.getRongIMClient()
						.sendImageMessage(
								getCurrentConversation().getConversationType(),
								getCurrentConversation().getTargetId(),
								content, null, null,
								new SendImageMessageCallback() {

									@Override
									public void onSuccess(Message arg0) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onProgress(Message arg0,
											int arg1) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onError(Message arg0,
											ErrorCode arg1) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onAttached(Message arg0) {
										// TODO Auto-generated method stub

									}
								});
			}
		}
	}

}
